using System;
using System.Data;
using System.Configuration;
using System.Text; //For StringBuilder
using AjaxNinja.Data;
using AjaxNinja.Types;
using Snap.Diagnostics;

/// <summary>
/// Summary description for InputProcessor
/// </summary>
namespace AjaxNinja.Business
{
    public class InputProcessor
    {
        private SQLDb db;
        private Diagnostics dg;

        public InputProcessor()
        {
            //Get our SQL Data writer ready
            db = new SQLDb();
            //Get our error trapping utility ready
            //NOTE: the Diagnostics class can be used with ANY ASP.NET application so
            //long as the tables and required stored procedure are available.
            //Thus when we initialize a diagnostics instance we have to provide a name
            //for the connection string since it's not tailored for a specific application
            dg = new Diagnostics("fbConnectionString");
        }

        public string ProcessInput(MadMessage msg, AppUser user)
        {
        
            StringBuilder sb = new StringBuilder("");
            bool error = false; //We'll need to know if there is an error message or not
            string errorMessages = "<ul>"; //Build a list of error messages incase multiple things go wrong
            try
            {
                if (msg.msg == default(string)) //No content
                {
                    //Error handling code here
                    error = true;
                    errorMessages += "<li>" + Resources.Messages.ErrorNoContent + "</li>";
                }
                else //We do indeed have content
                {
                    //Check the message length
                    if (msg.msg.Length > 200)
                    {
                        error = true;
                        errorMessages += "<li>" + Resources.Messages.ErrorTooLong + "</li>";
                    }

                    //Write the message to the database if we have had no errors
                    if(!error)
                        this.db.AddMessage(msg.msg, user.userID);
                }
            }
            catch (Exception er)
            {
                error = true;
                errorMessages += "<li>" + Resources.Messages.ErrorInternalServer + "</li>";
                SError logMsg = new SError("InputProcessor.cs", er.TargetSite.Name, "", 0, er.Message + er.StackTrace);
                dg.logError(logMsg);
            }

            //Now we need to determine the message that we're going to display
            //Whether it is a success or a failure, specifically
            if(error){ //If there has been an error
                sb.Append("<div class=\"error\">");
                sb.Append(errorMessages);
                sb.Append("</div>");
            }
            else{ //If we had no errors!
                sb.Append("<div class=\"success\">");
                sb.Append(Resources.Messages.MessagePostSubmit);
                sb.Append("</div>");
            }

            return sb.ToString(); //Release our string builder
        }
    }
}